﻿using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Interop;
using Microsoft.Win32;
using System.Collections.ObjectModel;


namespace Wosk
{
    /// <summary>
    /// Interaction logic for LaptopKeyboardView.xaml
    /// </summary>
    public partial class LaptopKeyboardView : Window
    {
        public VirtualKeyboard virtualKeyboard;
        //protected VirtualKeyboard virtualKeyboard;
        public LaptopKeyboardView()
        {
            InitializeComponent();
            virtualKeyboard = (VirtualKeyboard)this.FindResource("virtualKeyboard");
         ////   Brush buttonBack = new SolidColorBrush(Color.FromArgb(0x60, 0x80, 0x80, 0x90));
         //      foreach (Button btn in keyboard.Children) 
         //   {
         //       btn.Focusable = false;
         //       //    btn.Background = buttonBack;
            //   }        {
         
            SystemEvents.DisplaySettingsChanged += new
EventHandler(SystemEvents_DisplaySettingsChanged);
        }

        void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
        {
            this.Width  = 1024;
            this.WindowState = WindowState.Minimized;
            this.UpdateLayout();
            this.WindowState = WindowState.Normal;
            this.UpdateLayout();
        }

        private void TransparentWindow_Loaded(object sender, RoutedEventArgs e)
        {

            //HwndSource source = HwndSource.FromHwnd(new WindowInteropHelper(this).Handle);
            //source.AddHook(new HwndSourceHook(WndProc));
            // Get this window's handle
            IntPtr HWND = new WindowInteropHelper(this).Handle;

            //int WS_EX_TOOLWINDOW = 0x00000080;
            //int GWL_ID = (-12);
            //int GWL_STYLE = (-16);
            int GWL_EXSTYLE = (-20);
            //int WS_EX_DLGMODALFRAME = 0x0001;
            //uint SWP_NOSIZE = 0x0001;
            //uint SWP_NOMOVE = 0x0002;
            //uint SWP_NOZORDER = 0x0004;
            //uint SWP_FRAMECHANGED = 0x0020;
            //uint WM_SETICON = 0x0080;
            //long OLD = 
                NativeWin32.GetWindowLong(HWND, GWL_EXSTYLE);
            //            MessageBox.Show(OLD.ToString());
            // SetWindowLong(HWND, GWL_EXSTYLE, (IntPtr)(OLD | WS_EX_TOOLWINDOW));
            NativeWin32.SetWindowLong(HWND, GWL_EXSTYLE, (IntPtr)(0x8000000));//SetWindowPos(HWND, IntPtr.Zero, 0, 0, 0, 0, (uint)SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
            //    MessageBox.Show((OLD | 0x8000000).ToString());

            RegisterHotkey();
            lstKeysHistory.ItemsSource = KeysHistory;

        }

        private ManagedWinapi.Hotkey hotkey;
        private ObservableCollection<string> _keysHistory = new ObservableCollection<string>();

        public ObservableCollection<string> KeysHistory
        { get { return _keysHistory; } }


        private void RegisterHotkey()
        {
            hotkey = new ManagedWinapi.Hotkey();
            //  hotkey.WindowsKey = true;
            //  hotkey.KeyCode = System.Windows.Forms.Keys.Space;
            hotkey.KeyCode = System.Windows.Forms.Keys.NumLock;
            hotkey.HotkeyPressed += new EventHandler(hotkey_HotkeyPressed);
            try
            {
                hotkey.Enabled = true;
            }
            catch (ManagedWinapi.HotkeyAlreadyInUseException)
            {
                System.Windows.MessageBox.Show("Could not register hotkey (already in use).", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        void hotkey_HotkeyPressed(object sender, EventArgs e)
        {
            if (this.IsVisible) //(WindowState != WindowState.Minimized)
            {
                virtualKeyboard.ReleaseStickyKeys();    //sticky keys cause confusion when they are hidden but still active
                this.Hide();
                //WindowState = WindowState.Minimized;
            }
            else
            {
                KeysHistory.Clear();
                this.Show();
            }
        }

        private void Button_PressAndRelease(object sender, RoutedEventArgs e)
        {
                   Button btn = (Button)sender;
                   if (null != btn.CommandParameter)
                   {
                       string key = btn.CommandParameter.ToString();
                       virtualKeyboard.PressAndRelease(key);

                       if (lstKeysHistory.IsVisible) // do not add keys when history is disabled (i.e. when entering passwords)
                       {
                           KeysHistory.Add(key);
                           while (KeysHistory.Count > 20) KeysHistory.RemoveAt(0);
                       }
                   }
        }

        private void Button_PressAndHold(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            string key = "";
            if (null != btn.CommandParameter) key = btn.CommandParameter.ToString();
            virtualKeyboard.PressAndHold(key);
        }

        private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }


        private void Button_Hide(object sender, RoutedEventArgs e)
        {
            // it is better to have custom handler (like this) for hiding, 
            // instead of just mapping regular button/key (default is NumLock)
            // because hotkey can be sometimes broken.
            virtualKeyboard.ReleaseStickyKeys();
            //WindowState = WindowState.Minimized;
            this.Hide();
        }

        private void Button_Close(object sender, RoutedEventArgs e)
        {
            virtualKeyboard.ReleaseStickyKeys();
            this.Close();
        }

        private void TransparentWindow_Closed(object sender, EventArgs e)
        {
            virtualKeyboard.ReleaseStickyKeys();
            hotkey.Dispose();
        }

        private void TransparentWindow_Activated(object sender, EventArgs e)
        {
            KeysHistory.Clear();
            this.Show();
            //this.Activate();
        }

    }
}
